package cn.portx.frame;

import java.util.List;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf3.dorado.jpa.JpaUtil;
import com.bstek.bdf3.saas.domain.Organization;
import com.bstek.bdf3.security.orm.User;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;

import cn.portx.frame.entity.Company;

@Component
public class PersonCenterPR {

	@DataProvider
	@Transactional
	public Company getCompany() {
		User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Organization org = user.getOrganization();

		List<Company> list = JpaUtil.linq(Company.class).equal("orgId", org.getId()).list();
		if (list.isEmpty()) {
			Company c = new Company();
			c.setOrgId(org.getId());
			c.setFullName(org.getName());
			JpaUtil.persist(c);
			return c;
		} else {
			return list.get(0);
		}
	}

	@DataResolver
	@Transactional
	public void updateCompany(final Company c) {
		JpaUtil.merge(c);
	}
}
